#include "udp_proto.h"

int main (int argc, char **argv)
{
	struct sockaddr_in local_addr, remote_addr;
	socklen_t  remote_addr_len;
	struct msg_t buf;
	ssize_t cnt;

	if (argc < 3) {
		exit (1);
	}

	int fd = socket (AF_INET, SOCK_DGRAM, 0);
	if (-1 == fd) {
		perror ("socket()");
		exit (1);
	}

	//绑定本地地址
	remote_addr.sin_family  = AF_INET;
	remote_addr.sin_port = htons (atoi(SERVER_PORT));
	//将点分十进制的地址字符串转换为地址结构体
	inet_aton (SERVER_IP, &remote_addr.sin_addr);
#if 0
	if (-1 == bind (fd, (struct sockaddr *)&local_addr, sizeof (local_addr)))
	{
		perror ("bind()");
		goto ERROR;
	}
#endif
	//注意：在发送数据之前需要先确定好地址长度
	remote_addr_len  = sizeof (remote_addr);

	buf.id = atoi(argv[1]);
	strncpy (buf.name, argv[2], NAMESIZE);
	sendto (fd, &buf, sizeof(int8_t)+strlen(argv[2]) + 1, 0, (void *)&remote_addr, remote_addr_len);

	close (fd);
	return 0;
ERROR:
	close (fd);
}
